
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 票种api
 */
@Api(tags = "TicketType",description = "票种")
@RestController
public class TicketTypeController {
    
    @Autowired
    private TicketTypeDao dao;
   
    @Autowired
    private TicketDao ticketDao;
   
    /**
     * 获取票种集合
     * @return api结果
     */
    @ApiOperation(value = "获取票种集合")
    @RequestMapping(value =  "/api/TicketType/",method = {RequestMethod.GET})
    public ApiResult<List<TicketType>> Get(){
         ApiResult<List<TicketType>> result=new ApiResult<List<TicketType>>();
        try 
        {
            List<TicketType> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询票种集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询票种集合")
    @RequestMapping(value =  "/api/TicketType/Search",method = {RequestMethod.POST})
    public ApiResult<List<TicketType>> Search(@RequestBody PageTicketType model){
         ApiResult<List<TicketType>> result=new ApiResult<List<TicketType>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"Ticket_Type_Id") ;            
            Page<TicketType> page = dao.findByWhere(model.getName(),model.getProjectId(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID获取票种信息
     * @param id 票种ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取票种信息")
    @RequestMapping(value =  "/api/TicketType/{id}",method = {RequestMethod.GET})
    public ApiResult<TicketType> Get(@PathVariable Long id){
        ApiResult<TicketType> result=new ApiResult<TicketType>();
        try
        {
            TicketType model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改票种信息
     * @param id 票种ID
     * @param model 票种信息
     * @return api结果
     */
    @ApiOperation(value = "修改票种信息")
    @RequestMapping(value =  "/api/TicketType/{id}",method = {RequestMethod.PUT})
    public ApiResult<TicketType> Edit(@PathVariable Long id,@RequestBody TicketType model){
        ApiResult<TicketType> result=new ApiResult<TicketType>();
        try
        {
            TicketType door=  dao.findByProjectIdAndName(model.getProjectId(),model.getName());

            if(door!=null&&!door.getTicketTypeId().equals(id)){
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }else{
               model.setTicketTypeId(id);
               model.setCreateDate(new Date());
               dao.save(model);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取票类型集合
     * @param projectId 项目ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取票类型集合")
    @RequestMapping(value =  "/api/TicketType/Project/{projectId}",method = {RequestMethod.GET})
    public ApiResult<List<TicketType>> Project(@PathVariable String projectId){
        ApiResult<List<TicketType>> result=new ApiResult<List<TicketType>>();
        try
        {
            List<TicketType> model= dao.findByProjectId(projectId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加票种信息
     * @param model 票种信息
     * @return api结果
     */
    @ApiOperation(value = "添加票种信息")
    @RequestMapping(value =  "/api/TicketType/",method = {RequestMethod.POST})
    public ApiResult<TicketType> Add(@RequestBody TicketType model){
         ApiResult<TicketType> result=new ApiResult<TicketType>();
        try
        {
            if(dao.findByProjectIdAndName(model.getProjectId(),model.getName())==null){
                model.setCreateDate(new Date());
                dao.save(model);
            }else{
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除票种信息
     * @param id 票种ID
     * @return api结果
     */
    @ApiOperation(value = "删除票种信息")
    @RequestMapping(value =  "/api/TicketType/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            if(ticketDao.countByTicketTypeId(id)>0){
                result.setHasError(true);
                result.setMessage("该类型下有票，请先删除子级");
            }else{
                dao.deleteById(id);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

